package org.hu.vhr.controller.emp;

import org.hu.vhr.model.*;
import org.hu.vhr.service.*;
import org.hu.vhr.util.POIUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("/emp/basic")
public class EmpBacisController {


    @Autowired
    EmpBasicService empBasicService;
    @Autowired
    NationService nationService;
    @Autowired
    PoliticService politicService;
    @Autowired
    JobLevelService jobLevelService;
    @Autowired
    PositionService positionService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    RabbitTemplate rabbitTemplate;


    @GetMapping("/")
    public RespPage getEmployeeByPage(@RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer size,
                                      String keyWord){
        return empBasicService.getEmployeeByPage(page,size,keyWord);
    }

    @PostMapping("/")
    public RespBean addEmployee(@RequestBody Employee employee){
        if(empBasicService.addEmployee(employee) == 1){
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @DeleteMapping("/{id}")
    public RespBean deleteEmpByEid(@PathVariable Integer id){
        if(empBasicService.deleteEmpByEid(id)==1){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @PutMapping("/")
    public RespBean updateEmp(@RequestBody Employee employee){
        if(empBasicService.updateEmp(employee)==1){
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @GetMapping("/nation")
    public List<Nation> getAllNation(){
        return nationService.getAllNation();
    }

    @GetMapping("/politic")
    public List<Politicsstatus> getAllPolitic(){
        return politicService.getAllPolitic();
    }

    @GetMapping("/joblevel")
    public List<Joblevel> getAllJobLevel(){
        return jobLevelService.getAllJobLevel();
    }

    @GetMapping("/position")
    public List<Position> getAllPosition(){
        return positionService.getAllPositions();
    }

    @GetMapping("/maxWorkId")
    public RespBean maxWorkId(){
        return RespBean.success("",String.format("%08d",empBasicService.getMaxWorkId()+1));
    }

    @GetMapping("/deps")
    public List<Department> getAllDeps(){
        return departmentService.getAllDepsByParentId(-1);
    }

    @GetMapping("/export")
    public ResponseEntity<byte[]> exportData(){
        List<Employee> list = (List<Employee>) empBasicService.getEmployeeByPage(null,null,null)
                .getData();
        return POIUtils.employee2Excel(list);
    }

    @PostMapping("/import")
    public RespBean importData(MultipartFile file) throws IOException {
        List<Employee> list = POIUtils.excel2Employee(file,nationService.getAllNation(),politicService.getAllPolitic(),
                                                        positionService.getAllPositions(),jobLevelService.getAllJobLevel(),departmentService.getAllDeps());

        if(empBasicService.addEmps(list)==list.size()){
            return RespBean.success("导入成功");
        }
        return RespBean.error("导入失败");

    }


}
